<template>
  <el-row>
    <el-col :span="5" style="border-right: 1px solid #dfe6ec">
      <!-- <div class="comp-tree"> 
        <el-button class="comp-tr-top" type="primary" size="small" @click="handleAddTop" style="width: 100%"> 创建治理规范根目录
        </el-button>
        <el-input placeholder="输入关键字进行过滤~" v-model="filterText"></el-input>
        <el-tree ref="SlotTree" :data="treeList" :props="defaultProps" :current-node-key="currentNodekey"
          :filter-node-method="filterNode" :highlight-current="true" :expand-on-click-node="false" :node-key="NODE_KEY"
          :default-expanded-keys="showKey" @node-click="handleNodeClick">
          <div class="comp-tr-node" slot-scope="{ node, data }" style="width: 5rem">
            <i v-if="data.parentId === '0'" class="el-icon-folder"></i>
            <i v-else class="el-icon-collection-tag"></i>
            <span style="margin-left: 10px"></span>
            <template v-if="node.isEdit">
              <el-input v-model="data.name" autofocus size="mini" :ref="'slotTreeInput' + data[NODE_KEY]"
                @blur.stop="handleInput(node, data)" @keyup.enter.native="handleInput(node, data)"></el-input>
            </template>

<template v-else>
              <span :class="[data[NODE_KEY] < NODE_ID_START ? 'is-new' : '', 'comp-tr-node--name']">
                {{ node.label }}
              </span>
              <el-dropdown placement="top" style="margin-left: 10%">
                <span class="el-dropdown-link">
                  <i class="el-icon-more el-icon--right"></i>
                </span>
                <el-dropdown-menu>
                  <el-tooltip class="item" effect="dark" content="添加子节点" placement="top">
                    <el-button icon="el-icon-plus" size="large" circle title="添加子节点" @click="handleAdd(node, data)"
                      style="border: none; background-color: transparent; transform: scale(1.1)"></el-button>
                  </el-tooltip>
                  <el-tooltip class="item" effect="dark" content="查看详情" placement="top">
                  
                    <el-button icon="el-icon-document" size="large" circle title="查看节点详情"
                      @click="handleLook(node, data)"
                      style="border: none; background-color: transparent; transform: scale(1.1)"></el-button>
                  </el-tooltip>
                  <el-tooltip class="item" effect="dark" content="编辑节点" placement="top">
                    <el-button icon="el-icon-edit" size="large" circle title="编辑节点" @click="handleEdit(node, data)"
                      style="border: none; background-color: transparent; transform: scale(1.1)"></el-button>
                  </el-tooltip>
                  <el-tooltip class="item" effect="dark" content="删除节点" placement="top">
                    <el-button icon="el-icon-delete" size="large" circle title="删除节点"
                      @click="handleDeletetree(node, data)"
                      style="border: none; background-color: transparent; transform: scale(1.1)"></el-button>
                  </el-tooltip>
                </el-dropdown-menu>
              </el-dropdown>
  
            </template>
</div>
</el-tree>
<treeData ref="treeForm" @getTreeList="getTreeList" />
</div> -->
      <customTree :ancestralId="listQuery.ancestralId" :btnTitle="'创建治理规范根目录'" @getId="getId" @getTree="getTree" :type="true" :expandType="'1'" />
    </el-col>
    <el-col :span="19">
      <div class="app-container">
        <div class="filter-container">
          <div class="filter-item">
            <span class="span">标准名称:</span>
            <el-input v-model="listQuery.name" placeholder="请输入标准名称" style="width: 200px" />
          </div>
          <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
            {{ $t('table.search') }}
          </el-button>
          <el-button v-waves plain v-permission="['standardsystem:ModelSpecification:add']" class="filter-item" style="margin-left: 10px" type="primary" icon="el-icon-edit" @click="handleCreate">
            {{ $t('table.add') }}
          </el-button>
          <el-button
            v-waves
            plain
            v-permission="['standardsystem:ModelSpecification:delete']"
            class="filter-item"
            style="margin-left: 10px"
            type="danger"
            icon="el-icon-delete"
            @click="handleBathDelete"
          >
            {{ $t('table.delete') }}
          </el-button>

          <el-button v-waves class="filter-item" icon="el-icon-refresh" @click="handleRest">{{ $t('table.rest') }}</el-button>
        </div>

        <el-table ref="table" :key="tableKey" v-loading="listLoading" element-loading-text="正在加载，请稍后" style="z-index: 0;" :data="list" border fit highlight-current-row @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" />
          <el-table-column type="index" width="50" label="序号" align="center" />
          <el-table-column label="标准名称" align="center" min-width="350px">
            <template slot-scope="{ row }">
              <span>{{ row.name }}</span>
            </template>
          </el-table-column>
          <el-table-column label="标准版本号" :show-overflow-tooltip="true" align="center" min-width="150px">
            <template slot-scope="{ row }">
              <span>{{ row.version }}</span>
            </template>
          </el-table-column>
          <el-table-column label="标准发布时间" :show-overflow-tooltip="true" align="center" min-width="150px">
            <template slot-scope="{ row }">
              <span>{{ row.releaseTime.split(' ')[0] }}</span>
            </template>
          </el-table-column>
          <el-table-column label="发布机构" :show-overflow-tooltip="true" align="center" min-width="150px">
            <template slot-scope="{ row }">
              <span>{{ row.releaseInstitution }}</span>
            </template>
          </el-table-column>
          <el-table-column label="发布状态" :show-overflow-tooltip="true" align="center" min-width="150px">
            <template slot-scope="{ row }">
              <span>{{ row.releaseStatus | dictLabel('res_status') }}</span>
            </template>
          </el-table-column>

          <el-table-column label="操作" fixed="right" align="center" width="230" class-name="small-padding fixed-width">
            <template slot-scope="{ row }">
              <el-button v-permission="['standardsystem:ModelSpecification:update']" type="text" size="small" icon="el-icon-edit" @click="handleUpdate(row)">
                {{ $t('table.edit') }}
              </el-button>
              <el-button type="text" size="small" icon="el-icon-s-promotion" @click="publish(row)" v-if="row.releaseStatus == 1"> 发布 </el-button>
              <el-button type="text" size="small" icon="el-icon-close" @click="stop(row)" v-if="row.releaseStatus == 0"> 废止 </el-button>
              <el-button type="text" size="small" icon="el-icon-download" @click="down(row)"> 下载 </el-button>
              <el-button v-permission="['standardsystem:ModelSpecification:delete']" type="text" size="small" icon="el-icon-delete" class="delete-text-btn" @click="handleDelete(row)">
                {{ $t('table.delete') }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" :page-sizes="pageArray" @pagination="getList" />

        <governanceNormsForm ref="form" @refreshList="getList" />
      </div>
    </el-col>
  </el-row>
</template>

<script>
import { fetchGovernanceNormsList, upDown, deleteTable, release } from '@/api/standardsystem/governanceNorms'
import waves from '@/directive/waves' // waves directive
import permission from '@/directive/permission/permission'
import governanceNormsForm from './governanceNormsForm'
import Pagination from '@/components/Pagination'
import customTree from '@/components/customTree/index.vue'

export default {
  name: 'ModelSpecificationList',
  components: { governanceNormsForm, Pagination, customTree },
  directives: { waves, permission },
  data() {
    return {
      tableKey: 0,
      treeList: [],
      list: null,
      total: 0,
      multipleSelection: [],
      listLoading: true,
      pageArray: this.$store.getters.pageArray,
      listQuery: {
        ancestralId: '1',
        page: 1,
        limit: this.$store.getters.defaultPageSize,
      },
      activeName: 'first', //tarbar
      NODE_KEY: 'id', // id对应字段
      MAX_LEVEL: 5, // 设定最大层级
      NODE_ID_START: 0, // 新增节点id，逐次递减
      startId: null,
    }
  },
  computed: {
    ids() {
      const ids = []
      this.multipleSelection.forEach((ele) => {
        ids.push(ele.id)
      })
      return ids
    },
  },
  methods: {
    // 树形列表
    getId(data) {
      this.listQuery.ancestralId = '1'
      this.listQuery.directoryId = data.id
      this.getList()
    },
    getTree(data) {
      this.treeList = data
    },
    // funindex(data) {
    //   var ids = []
    //   data.forEach((item) => {
    //     // 将当前对象的 id 添加到 ids 数组中
    //     ids.push(item.id)
    //     // 如果当前对象有 children 属性并且是一个数组，则递归调用 getAllIds 函数
    //     if (item.children && Array.isArray(item.children)) {
    //       // 递归调用 getAllIds 函数，将结果合并到 ids 数组中
    //       ids = ids.concat(this.funindex(item.children))
    //     }
    //   })
    //   return ids
    // },

    // 表格
    getList() {
      this.listLoading = true
      fetchGovernanceNormsList(this.listQuery).then((response) => {
        if (response.data.code == 0) {
          this.list = response.data.data
          this.total = response.data.total
          this.listLoading = false
        } else {
          this.$message.error(response.data.msg)
          this.listLoading = false
        }
      })
    },
    // 创建
    handleCreate() {
      this.$refs.form.setList(this.treeList)
      this.$refs.form.handleCreate()
    },
    // 编辑
    handleUpdate(row) {
      this.$refs.form.setList(this.treeList)
      this.$refs.form.handleUpdate(row.id)
    },
    // 发布
    publish(row) {
      this.$confirm('确定发布吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        release({ id: row.id, releaseStatus: 0 }).then((response) => {
          if (response.data.code === 0) {
            this.getList()
            this.$message.success('发布成功')
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },
    // 废止
    stop(row) {
      this.$confirm('确定废止吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        release({ id: row.id, releaseStatus: 1 }).then((response) => {
          if (response.data.code === 0) {
            this.getList()
            this.$message.success('废止成功')
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },
    // 下载
    down(row) {
      this.$confirm('确定下载所选数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        upDown({ ids: row.id }).then((response) => {
          // console.log(response,'1111')
          if (response.data.code == 200) {
            const objectUrl = response.data.data[0]
            const link = document.createElement('a')
            // 设置导出的文件名称
            // link.download = `治理规范` + '.xlsx'
            link.style.display = 'none'
            link.href = objectUrl
            link.click()
            document.body.appendChild(link)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },
    // 表格删除
    handleDelete(row) {
      let list = []
      list.push(row.id)
      this.$confirm('确定删除该数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        const params = { idList: list }
        deleteTable(params).then((response) => {
          if (response.data.code === 0) {
            this.getList()
            this.$message.success(response.data.msg)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },
    // 表格多选
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    //重置
    handleRest() {
      this.listQuery = {
        directoryId: this.listQuery.directoryId,
        page: this.listQuery.page,
        limit: this.$store.getters.defaultPageSize,
      }
      this.getList()
    },
    // 删除
    handleBathDelete() {
      if (this.ids === '') {
        this.$message.warning('请选择至少一条数据')
        return
      }
      this.$confirm('确定删除所选数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        // ===================批量删除
        const params = { idList: this.ids }
        deleteTable(params).then((response) => {
          if (response.data.code === 0) {
            this.getList()
            this.$message.success(response.data.msg)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },
    // 搜索
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
  },
}
</script>
<style lang="scss" scoped>
::v-deep(::-webkit-scrollbar) {
  display: block !important;
}

#treeBox {
  margin-top: 1px;

  .el-table thead {
    display: none;
  }
}

.span {
  color: #606266;
  font-weight: 700;
  font-size: 14px;
}
</style>
<style scoped>
.icon {
  width: 10px;
  height: 10px;
  /*background:url('../img/icon.jpg');*/
  background-size: 100% 100%;
}

.el-tree {
  width: 100% !important;
  min-width: auto !important;
}

.el-tree-node__content {
  height: 32px !important;
}

.el-tree-node .is-current .is-focusable {
  background: rebeccapurple !important;
}

.el-row {
  display: flex;
}

.el-col-5 {
  flex: 1;
}
</style>
